package org.zjut.pso;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Rising
 * @date 2017/9/23
 * @des  算法基础
 */
public class PSOBase extends JdbcDaoSupport{
    private IPSO mPSO;   //算法
    String dataBase = "pso_recommond";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //必须初始化，决定使用哪种推荐算法
    //这里用PSOone算法，只是个demo
    public void setmPSO(IPSO mPSO) {
        this.mPSO = mPSO;
    }

    /**
     * 保存被推荐的知识点
     * @return
     */
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean saveRecommondKnowledge(Map<String,Object> params){

        List<Map<String,Object>> knoledges = mPSO.getRecommondKnowledge(params);
        System.out.println("knowledges size is "+knoledges.size());

        String sql = "INSERT INTO "+dataBase+".learner_recommon_knowledge (userCode,courseCode,knowledgeCode,isFinish,serverTime)" +
                "VALUES( ?,?,?,?,?) ";
        if(knoledges!=null&&knoledges.size()>0){
            try {
                //插入之前先删除原来的同样用户的同样课程
                delRecommondKnowledge(params.get("userCode").toString(),params.get("courseCode").toString());
                List<Object[]> objs =new ArrayList<>();
                for(Map<String,Object> item : knoledges){
                    String learner,courseCode,knowledgeCode;
                    learner = params.get("userCode").toString();
                    courseCode = params.get("courseCode").toString();
                    knowledgeCode = item.get("conceptCode").toString();
                    Object[] obj = new Object[]{learner,courseCode,knowledgeCode,0,sdf.format(new Date())};
                    objs.add(obj);
                }
                getJdbcTemplate().batchUpdate(sql,objs);
                return true;
            }catch (Exception e){
                System.out.println(e.getMessage()+" saveRecommondKnowledge");
                throw e;
            }
        }else{
            return false;
        }
    }

    /**
     * 保存推荐给用户的资源信息
     * @param params
     * @return
     */
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean saveRecommondResource(Map<String,Object> params){
        List<Map<String,Object>> resources = mPSO.getRecommondResource(params);

        String sql = "INSERT INTO "+dataBase+".learner_recommon_resource (userCode,knowledgeCode,resourceCode,isFinish,serverTime)" +
                "VALUES( ?,?,?,?,?) ";

        //同时，往用户和课程的关系表 learner_course表里面，插入关系
        String sql2 ="INSERT INTO "+dataBase+".learner_course (learnerCode,courseCode,status,serverTime) " +
                "VALUES(?,?,?,?)";
        if(resources!=null&&resources.size()>0) {
            try {
                //先删除原来推荐过的同样课程的资源
                delRecommondResource(params.get("userCode").toString(),params.get("courseCode").toString());

                List<Object[]> objs = new ArrayList<>();
                for (Map<String, Object> item : resources) {
                    String userCode, knowledgeCode, resourceCode;
                    userCode = params.get("userCode").toString();
                    knowledgeCode = item.get("conceptCode").toString();
                    resourceCode = item.get("resourceCode").toString();
                    Object[] obj = new Object[]{userCode, knowledgeCode, resourceCode, 0, sdf.format(new Date())};
                    objs.add(obj);
                }
                getJdbcTemplate().batchUpdate(sql, objs);
                //同时，往用户和课程的关系表 learner_course表里面，插入关系
                getJdbcTemplate().update(sql2,new Object[]{params.get("userCode").toString(),params.get("courseCode").toString(),1,sdf.format(new Date())});
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage() + " ");
                throw e;
            }
        }
        return false;
    }

    /**
     * 删除之前推荐的某课程下面的知识点信息
     * @param userCode
     * @param courseCode
     */
    public void delRecommondKnowledge(String userCode,String courseCode){
        String sql = "DELETE FROM "+dataBase+".learner_recommon_knowledge WHERE knowledgeCode = ? AND courseCode = ? ";
        try {
            getJdbcTemplate().update(sql,new Object[]{userCode,courseCode});
        }catch (Exception e){
            System.out.println("delRecommondKnowledge:"+e.getMessage());
        }
    }

    /**
     * 删除之前推荐的某课程下面的资源信息
     * @param userCode
     * @param courseCode
     */
    public void delRecommondResource(String userCode,String courseCode){
        String sql = "DELETE FROM "+dataBase+".learner_recommon_resource WHERE learnerCode = ? AND knowledgeCode in (" +
                "SELECT a.code  FROM "+dataBase+".knowledge as a WHERE a.courseCode = ? ) ";
        try {
            System.out.println(sql);
            getJdbcTemplate().update(sql,new Object[]{userCode,courseCode});
        }catch (Exception e){
            System.out.println("delRecommondKnowledge:"+e.getMessage());
        }
    }
}
